//
//  MYGMedicalToolsScoreView.swift
//  MYDoctor
//
//  Created by 林矗 on 2024/4/21.
//

import UIKit
import QMUIKit
class MYGMedicalToolsScoreView: UIView {

    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    var progress: Double = 0.0 {
        didSet {
            if progress < 0  {
                progress = 0
            }
            
            if progress > 1 {
                progress = 1
            }
            pieView.progress = progress
        }
    }
    
    var title: String = "" {
        didSet {
            pieView.title = title
        }
    }
    
    var level: String = "" {
        didSet {
            pieView.level = level
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        addSubviews([
            titleLabel,
            pieView
        ])
        
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(26)
        }
        
        pieView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-26)
            make.size.equalTo(CGSize(width: 80, height: 80))
        }
    }
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "得分"
        label.font = .medium(25)
        label.textColor = .rgba(51, 51, 51)
        return label
    }()
    
    
    private lazy var pieView: MYGMedicalToolsScoreProgressView = {
        let view = MYGMedicalToolsScoreProgressView()
        view.lineWidth = 5.0
        view.ringColor = .rgba(234, 77, 96)
        view.bgRingColor = .rgba(234, 77, 96, 0.2)
        view.progress = 0
        view.title = "7"
        view.level = "高危"
        return view
    }()
    
}


class MYGMedicalToolsScoreProgressView: UIView {
    // MARK: - Propert
    
    var lineWidth = 5.0 {
        didSet {
            progressView.lineWidth = lineWidth
            bgProgressView.lineWidth = lineWidth
        }
    }
    var progress: Double = 0.0 {
        didSet {
            progressView.setProgress(Float(progress), animated: true)
        }
    }
    
    var title: String = "" {
        didSet {
            titleLabel.text = title
        }
    }
    
    var level: String = "" {
        didSet {
            label.text = level
        }
    }
    
    var bgRingColor: UIColor = .rgba(242, 239, 239, 1)  {
        didSet {
            bgProgressView.tintColor = bgRingColor
        }
    }
    
    var ringColor: UIColor = .rgba(133, 136, 176, 1) {
        didSet {
            progressView.tintColor = ringColor
        }
    }
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        configUI()
    }
    
    // MARK: - UI
    private func configUI() {
        addSubviews([
            bgProgressView,
            progressView,
            titleLabel,
            label
        ])
        
        bgProgressView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        progressView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        label.snp.makeConstraints { make in
            make.bottom.equalTo(-20)
            make.centerX.equalToSuperview()
            make.left.right.equalToSuperview().inset(6)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.centerX.equalToSuperview()
            make.left.right.equalToSuperview().inset(6)
        }
    }
    
    // MARK: - Public funcs
    
    // MARK: - lazy load
    private lazy var bgProgressView: QMUIPieProgressView = {
        let view = QMUIPieProgressView()
        view.shape = .ring
        view.borderWidth = 0
        view.lineWidth = 5
        view.backgroundColor = .clear
        view.tintColor = .rgba(242, 239, 239, 1)
        view.transform = CGAffineTransform(rotationAngle: Double.pi)
        view.setProgress(1, animated: false)
        return view
    }()
    
    private lazy var progressView: QMUIPieProgressView = {
        let view = QMUIPieProgressView()
        view.shape = .ring
        view.borderWidth = 0
        view.lineWidth = 5
        view.backgroundColor = .clear
        view.tintColor = .rgba(133, 136, 176, 1)
        view.transform = CGAffineTransform(rotationAngle: Double.pi)
        view.setProgress(0, animated: false)
        return view
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(20)
        label.textColor = .rgba(67, 67, 67)
        label.textAlignment = .center
        return label
    }()
    
    private lazy var label: UILabel = {
        let label = UILabel()
        label.font = .regular(13)
        label.textColor = .rgba(67, 67, 67)
        label.textAlignment = .center
        return label
    }()
}

